Utilizing Sieve scripts to organize email, locally
Step one: get a sieve script on your disk. I wrote sieve-kde-download.rb to
download all of them from my server.
Then run the sieve-filter command like so:
sieve-filter -Cv -o mail_location=maildir:$HOME/mail:LAYOUT=fs $HOME/.config/sieve/.active.sieve INBOX/Github
Where…
- the first file path is the currently active sieve script, the one you want
to run. mail_locationis a path to your local copy of the email. Offlineimap can
help with that.- INBOX is the name of the folder, as would be represented to IMAP clients.
For example, the subfolder ‘Family’ would be represented as ‘INBOX/Family’.
NOTE: Ensure that your offlineimap is configured to use / as the hiearachy separator
rather than a period. If you already did a sync with that, like I did, here’s a
quick one-liner in ruby to shuffle the directories into the correct
subdirectory:
cd ~/mail
ruby -ve 'Hash[Dir["*"].map { |d| [d, d.gsub(/\./, "/")] }].each { |k, v| FileUtils.move(k, v) if k != v and not k.include? "dovecot" }'
Reformatted for readability:
# A hash that looks like {"Archive"=>"Archive", "INBOX"=>"INBOX", "INBOX.Family"=>"INBOX/Family"}
directory_translations = Hash[Dir["*"].map { |dir| [dir, dir.gsub(/\./, "/")] }]
directory_translations.each do |old_name, new_name|
if old_name != new_name # Don't try to move a folder into itself
unless old_name.include? "dovecot" # skip the files that dovecot makes when running sieve-filter
FileUtils.move(old_name, new_name)
end
end
end
And an example configuration of offlineimap:
[Repository Local]
type = Maildir
localfolders = ~/mail
sep = /
# etc...
Actually going for it
Now that you’ve ran sieve-filter at least once in debug/non-execution mode,
and seen what it will do, it’s time then to actually effect change in your
mailbox.
To do so, run the same command, but now with the -e flag. Easy, but this is
the scary moment! Hope your sieve script is correctly configured!
You may want to clean up all of the crap dovecot leaves behind:
fd -0 dovecot mail/ | xargs -0 rm
Preparing to be able to run sieve-filter
Of course it’s not so easy, you have to do the following:
-
pacman -S pigeonholewhich will install dovecot and it’s sieve extension -
sudo cp -r /usr/share/doc/dovecot/example-config /etc/dovecotso that there
is some sort of configuration, any kind, on the system -
Edit /etc/dovecot/conf.d/90-sieve.conf - set a value you’re happy with for
where the sieve scripts will live. I like not having a ton of dotfiles in my
home dir, so I changed it like so:plugin { sieve = file:~/.config/sieve;active=~/.config/sieve/.active.sieve }
Further documentation can be found at Dovecot’s website.
Additional sieve examples
here are some links to interesting, additional sieve scripts for your reference:
- (check logseq notes)